package com.heima.test;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.article.feign.ApArticleFeign;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ApArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.comment.Vo.WmCommentPage;
import com.heima.model.comment.dtos.ApCommentDto;
import com.heima.model.comment.dtos.ApCommentRepayDtoA;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentLike;
import com.heima.model.comment.pojos.ApCommentRepay;
import com.heima.model.wemedia.dtos.WmCommentApplyDto;
import com.heima.model.wemedia.dtos.WmCommentDto;
import com.heima.model.wemedia.dtos.WmCommentPageReqDto;

import com.heima.utils.common.BeanHelper;
import com.heima.utils.common.DateUtils;
import com.heima.wemedia.WemediaApplication;
import com.heima.wemedia.mapper.WmCommentMapper;
import com.heima.wemedia.service.WmCommentService;
import org.apache.kafka.common.protocol.types.Field;
import org.apache.poi.hssf.record.DVALRecord;
import org.checkerframework.checker.units.qual.A;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.jdbc.object.SqlQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.security.acl.Group;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Xu
 * @Date: 2022/01/03/16:23
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WemediaApplication.class)
public class Comment {
    @Autowired
    private WmCommentMapper wmCommentMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ApArticleFeign apArticleFeign;
    @Autowired
    private WmCommentService wmCommentService;

    @Test
    public void test_MongoDB(){

        Query query = Query.query(Criteria.where("authorId").is(4));
        //按照时间倒序发布排列
        query.with(Sort.by(Sort.Direction.DESC,"createdTime"));
        //分页查询
//        query.limit(5).skip(2);

        Criteria criteria = new Criteria();
        criteria.and("createdTime").lt(DateUtils.stringToDate("2021-06-10 12:50:54.000"));
        query.addCriteria(criteria);
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
        apComments.forEach(System.out::println);

    }

    @Test
    public void test_inset(){
        ApComment apComment = new ApComment();
//        apComment.setId();
        apComment.setAuthorId(4);
        apComment.setAuthorName("admin");
        apComment.setEntryId(1476180997337911297L);
        apComment.setChannelId(1);
//        apComment.setType((short)0);
        apComment.setContent("测试评论");
        apComment.setImage("meitouxiang");
        apComment.setLikes(10);
        apComment.setReply(0);
        apComment.setFlag((short)0);
        apComment.setOrd(1);
        apComment.setCreatedTime(new Date());
        apComment.setUpdatedTime(new Date());
        apComment.setStatus(true);
//        apComment.setCount();
        for (int i = 0; i <10 ; i++) {

          while (i==9){
              mongoTemplate.save(apComment);
          }

        }



    }

    @Test
    public void test_GroupBY(){
        Criteria criteria = Criteria.where("authorId").is(4);






//         criteria = Criteria.where("createTime").lte(new Date().getTime());


        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria));
        AggregationResults<ApComment> monitor = mongoTemplate.aggregate(aggregation, "monitor", ApComment.class);
        List<ApComment> mappedResults = monitor.getMappedResults();
        mappedResults.forEach(System.out::println);

    }

    @Test
    public void test_MongoDB1(){
        String comments = "comments";
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("authorId").is(4)),
                Aggregation.group("entryId").count().as(comments)

        );
        AggregationResults<ApComment> apComents = mongoTemplate.aggregate(aggregation, "ApComents", ApComment.class);
        List<ApComment> mappedResults = apComents.getMappedResults();
        if (CollectionUtils.isNotEmpty(mappedResults)){
            ApComment apComment = mappedResults.get(1);
            System.out.println(apComents+"<------------->");
            System.out.println(apComment+"<------------->");
        }


    }
    @Test
    public void test_MongoDB2(){
        String comments = "comments";
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("authorId").is(4)),
                Aggregation.group("entryId").count().as(comments)

        );
        AggregationResults<ApComment> apComents = mongoTemplate.aggregate(aggregation, "ApComents", ApComment.class);
        List<ApComment> mappedResults = apComents.getMappedResults();
        if (CollectionUtils.isNotEmpty(mappedResults)){
            ApComment apComment = mappedResults.get(1);
            System.out.println(apComents+"<------------->");
            System.out.println(apComment+"<------------->");
        }




    }
    @Test
    public void test_MongoDB6(){
//        Query query = new Query(Criteria.where("entryId").is("1475103386180390914"))
//                .skip(0L)
//                .limit(10);
//        //获取初步的分页对象
//        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
//        apComments.forEach(System.out::println);
        /*Query query = new Query(Criteria.where("entryId").is("1475103386180390914"));
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
        apComments.forEach(System.out::println);*/

        Query query = Query.query(Criteria.where("entryId").is(1475103386180390914L));
        ApComment apComment = mongoTemplate.findOne(query, ApComment.class);
        System.out.println(apComment);
    }



    @Test
    public void test_3(){
//        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "createdTime");
//        Criteria criteria = Criteria.where("userId").is(4);
//        if (true){
//            criteria.and("createdTime").gte(DateUtils.stringToDate("2021-06-14 03:44:33.000"));
//            Criteria.where("createdTime").gte(DateUtils.stringToDate("2021-06-14 03:44:33.000"));
//        }
        //调用feign接口后去当前登录用户所有发布的文章Id集合
        ResponseResult<List<ApArticle>> articleByUserId = apArticleFeign.findArticleByUserId(1102L);
        List<ApArticle> apArticleList = articleByUserId.getData();
        List<Long> apArticleIds = apArticleList.stream().map(ApArticle::getId).collect(Collectors.toList());
//        apArticleIds.forEach(System.out::println);

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.project("authorId","entryId","createdTime"),
                //id为当前用户
//                    Aggregation.match(criteria),
                Aggregation.match(Criteria.where("authorId").is(4).and("createdTime").and("entryId").in().gte(DateUtils.stringToDate("2021-06-01 03:44:33.000"))),// 条件
//                Aggregation.match(Criteria.where("createdTime").gte("2021-06-09 03:44:33.000"),
                //按照倒序排序
                Aggregation.group("entryId").count().as("count")// 分组统计
                            .first("createdTime").as("createdTime")
                            .first("authorId").as("authorId")
                            .first("entryId").as("entryId"),
                //日期降序排列
                Aggregation.sort(Sort.by(Sort.Direction.DESC,"createdTime")),

                //分页查询
                Aggregation.skip(0),
                Aggregation.limit(10)

                );
        AggregationResults<ApComment> result = mongoTemplate.aggregate(aggregation, "ap_comment",ApComment.class);

        List<ApComment> mappedResults = result.getMappedResults();
        for (ApComment mappedResult : mappedResults) {

        }
        mappedResults.forEach(System.out::println);

    }

    @Test
    public void test_FeignTest(){
        ResponseResult<List<ApArticle>> articleByUserId = apArticleFeign.findArticleByUserId(1102L);
        List<ApArticle> apArticleList = articleByUserId.getData();
        List<Long> collect = apArticleList.stream().map(ApArticle::getId).collect(Collectors.toList());
//        collect.forEach(System.out::println);
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.project("authorId","entryId","createdTime"),
                //id为当前用户
//                    Aggregation.match(criteria),
                Aggregation.match(Criteria.where("entryId").in(collect)
                        .and("createdTime")
                        .gte(DateUtils.stringToDate("2022-01-01"))
                        .lte(DateUtils.stringToDate("2022-01-05"))),// 条件
//                Aggregation.match(Criteria.where("createdTime").gte("2021-06-09 03:44:33.000"),
                //按照倒序排序
                Aggregation.group("entryId").count().as("count")// 分组统计
                        .first("createdTime").as("createdTime")
                        .first("authorId").as("authorId")
                        .first("entryId").as("entryId"),
                //日期降序排列
                Aggregation.sort(Sort.by(Sort.Direction.DESC,"createdTime")),

                //分页查询
                Aggregation.skip(0L),
                Aggregation.limit(10L)

        );
        AggregationResults<ApComment> result = mongoTemplate.aggregate(aggregation, "ap_comment",ApComment.class);

        List<ApComment> mappedResults = result.getMappedResults();
        for (ApComment mappedResult : mappedResults) {

        }
        for (ApComment mappedResult : mappedResults) {
            System.out.println(mappedResult);
        }

        mappedResults.forEach(System.out::println);

    }

        @Test
        public void test_findArticlePage(){
            WmCommentApplyDto wmCommentApplyDto = new WmCommentApplyDto();
            wmCommentApplyDto.setBeginDate("2020-09-07 14:56:18");
            wmCommentApplyDto.setEndDate("2021-12-29 21:18:43");
            wmCommentApplyDto.setSize(10);
            wmCommentApplyDto.setPage(0);
            wmCommentApplyDto.setAuthorId(1102L);

//            ResponseResult newsCommentList = wmCommentService.findNewsCommentList(wmCommentApplyDto);
            ResponseResult articlePage = apArticleFeign.findArticlePage(wmCommentApplyDto);
            List<ApArticleDto> data = (List<ApArticleDto>)articlePage.getData();
            data.forEach(System.out::println);

        }
        @Test
        public void test_CommentList(){
            WmCommentPageReqDto dto = new WmCommentPageReqDto();
            dto.setArticleId(1476180997337911297L);
            dto.setSize(10);
            dto.setPage(1);
            Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));
            List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
            //将createTime以及UpdateTime设置为integet

            //转换为输出类型
//            List<ApCommentDto> apCommentDtoList = BeanHelper.copyWithCollection(apComments, ApCommentDto.class);
            apComments.forEach(System.out::println);
            //获取以及评论的ID  使用一级评论查询子评论
//            List<String> commentIdList = apCommentDtoList.stream().map(ApCommentDto::getId).collect(Collectors.toList());
            //根据一级评论目录查找耳机评论
            for (ApComment apComment : apComments) {
                Query query1 = new Query(Criteria.where("commentId").is(apComment.getId()));
                List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query1, ApCommentRepay.class);
                //将日期字符串拿出来拿出来 转换日期
//                for (ApCommentRepay apCommentRepay : apCommentRepays) {
//                    apComment.setApCommentRepays(apCommentRepays);
//                }

            }
            apComments.forEach(System.out::println);

//
//            for (String commentId : commentIdList) {
//
//                Query query1 = new Query(Criteria.where("commentId").is(commentId));
//                List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query, ApCommentRepay.class);
//                for (ApCommentDto apCommentDto : apCommentDtoList) {
//                    //如果二级评论的CommentId就是一级评论的Id主键
//                    if (commentId.equals(apCommentDto.getId())){
//                        apCommentDto.setCommentRepayList();
//                    }
//                }
//            }

        }

        @Test
        public void test_insert(){
                    //向onggoDb增加数据
            ApCommentRepay apCommentRepay = new ApCommentRepay();
            apCommentRepay.setId("61d58bc235b9c43dc6a4ce43");
            apCommentRepay.setAuthorId(5);
            apCommentRepay.setAuthorName("admin");
            apCommentRepay.setCommentId("61d39500c2c1fd4e24d3ba81");
            apCommentRepay.setContent("是呀是呀！楼上说的的对呀+2");
            apCommentRepay.setLikes(0);
            apCommentRepay.setCreatedTime(new Date());
            apCommentRepay.setUpdatedTime(new Date());
            ApCommentRepay save = mongoTemplate.save(apCommentRepay);
//        Map<List<ApComment>,List<ApCommentRepay>>   listMap = new HashMap();


        }

    @Test
    public void test_CommentListA(){
        WmCommentPageReqDto dto = new WmCommentPageReqDto();
        dto.setArticleId(1476180997337911297L);
        dto.setSize(10);
        dto.setPage(1);
        Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
        //将createTime以及UpdateTime设置为integet

        //转换为输出类型
            List<ApCommentDto> apCommentDtoList = BeanHelper.copyWithCollection(apComments, ApCommentDto.class);
            //将createTime由date转换成Long类型  完成封装
        for (ApCommentDto apCommentDto : apCommentDtoList) {
            for (ApComment apComment : apComments) {
                if (apCommentDto.getId().equals(apComment.getId())){
                    Date createdTime = apComment.getCreatedTime();
                    Date updatedTime = apComment.getUpdatedTime();
                    apCommentDto.setCreatedTime(createdTime.getTime());
                    apCommentDto.setUpdatedTime(updatedTime.getTime());
                }
            }
        }
//        apCommentDtoList.forEach(System.out::println);
        //获取以及评论的ID  使用一级评论查询子评论
//            List<String> commentIdList = apCommentDtoList.stream().map(ApCommentDto::getId).collect(Collectors.toList());
        //根据一级评论目录查找耳机评论
        for (ApCommentDto apCommentDto : apCommentDtoList) {
            Query query1 = new Query(Criteria.where("commentId").is(apCommentDto.getId()));
            List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query1, ApCommentRepay.class);
//            apCommentRepays.forEach(System.out::println);
            //将apCommentRepays 转换为ApCommentRepayDtoA类型
            List<ApCommentRepayDtoA> apCommentRepayDtoAS = BeanHelper.copyWithCollection(apCommentRepays, ApCommentRepayDtoA.class);
            for (ApCommentRepay apCommentRepay : apCommentRepays) {
                for (ApCommentRepayDtoA apCommentRepayDtoA : apCommentRepayDtoAS) {
                    if (apCommentRepay.getId().equals(apCommentRepayDtoA.getId())){
                        Date createdTime = apCommentRepay.getCreatedTime();
                        Date updatedTime = apCommentRepay.getUpdatedTime();
                        apCommentRepayDtoA.setCreatedTime(createdTime.getTime());
                        apCommentRepayDtoA.setUpdatedTime(updatedTime.getTime());
                    }
                }
            }
//            apCommentRepayDtoAS.forEach(System.out::println);

            //将转换为ApCommentRepayDtoA 放入ApCommentDto
            //将日期字符串拿出来拿出来 转换日期
            for (ApCommentRepayDtoA apCommentRepay : apCommentRepayDtoAS) {
                apCommentDto.setApCommentRepays(apCommentRepayDtoAS);
            }

        }
        apCommentDtoList.forEach(System.out::println);


//
//            for (String commentId : commentIdList) {
//
//                Query query1 = new Query(Criteria.where("commentId").is(commentId));
//                List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query, ApCommentRepay.class);
//                for (ApCommentDto apCommentDto : apCommentDtoList) {
//                    //如果二级评论的CommentId就是一级评论的Id主键
//                    if (commentId.equals(apCommentDto.getId())){
//                        apCommentDto.setCommentRepayList();
//                    }
//                }
//            }

    }

    @Test
    public void test_PageCommentB(){
        WmCommentPageReqDto dto = new WmCommentPageReqDto();
        dto.setArticleId(1476180997337911297L);
        dto.setSize(10);
        dto.setPage(1);
        Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));
        //查出一级评论
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);

        List<WmCommentPage> wmCommentPageList = new ArrayList<>();
        for (ApComment apComment : apComments) {
            WmCommentPage wmCommentPage = new WmCommentPage();
            //根据评论查询回复
            Query query1 = new Query(Criteria.where("commentId").is(apComment.getId()));
            List<ApCommentRepay> apCommentRepays = mongoTemplate.find(query1, ApCommentRepay.class);
            wmCommentPage.setApComments(apComment);
            if (apCommentRepays!=null){
                wmCommentPage.setApCommentRepays(apCommentRepays);
            }
            wmCommentPageList.add(wmCommentPage);

        }
        wmCommentPageList.forEach(System.out::println);


    }
    @Test
    public void test_CommentLikes(){
        Query queryLike = Query.query(Criteria.where("authorId").is(4)
                .and("commentId").is("61d394be3698737be9024470"));
        ApCommentLike one = mongoTemplate.findOne(queryLike, ApCommentLike.class);

        System.out.println(one.toString());

    }
    @Test
    public void test_MongoDB7(){
        Query query = Query.query(Criteria.where("entryId").is(1476180997337911297L));
//                .skip(0L)
//                .limit(10);
        //获取初步的分页对象
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
        apComments.forEach(System.out::println);

    }



}
